import Vue from 'vue';
import VueRouter from 'vue-router';
/* import VueCookies from 'vue-cookies'; */

Vue.use(VueRouter);

const routes = [
	{
		path: '/',
		redirect: '/login', // 默认重定向到登录页面
	},
	{
		path: '/login',
		name: 'login',
		component: () => import('../views/login/index.vue'),
	},
	{
		path: '/home',
		name: 'home',
		component: () => import('../views/home/index.vue'),
		children: [
			{
				path: '',
				name: 'home-index',
				component: () => import('../views/home/components/home.vue'),
			},
			{
				path: 'home',
				name: 'home-index',
				component: () => import('@/views/home/components/home.vue'),
			},
			{
				path: 'new',
				name: 'home-new',
				component: () => import('@/views/home/components/new.vue'),
			},
			{
				path: 'student',
				name: 'home-student',
				component: () => import('@/views/home/components/student.vue'),
			},
		],
	},
	{
		path: '/register',
		name: 'register',
		component: () => import('@/views/register/index.vue'),
	},
];

const router = new VueRouter({
	routes,
});

// 添加路由守卫
/* router.beforeEach((to, from, next) => {
	// 白名单路径（允许未登录访问）
	const whiteList = ['/login'];

	// 检查登录状态（从cookies获取token）
	const hasToken = VueCookies.get('token');

	if (hasToken) {
		// 已登录状态
		if (to.path === '/login') {
			next(); // 正常访问
		}
	} else {
		// 未登录状态
		if (whiteList.includes(to.path)) {
			next(); // 允许访问白名单页面
		} else {
			// 重定向到登录页
			router.push(`/login?redirect=${to.path}`);
			// 显示提示信息
			MessageBox.alert('请先登录系统');
		}
	}
}); */

export default router;
